package com.xbongbong.workorder.abstracts;

import com.xbongbong.paas.abstracts.AbstractLowCodeExecuteService;
import com.xbongbong.paas.business.rule.FormDataInactiveRuleByDeleteBatchHelp;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.InactiveRuleByDeleteBatchDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.parent.abstracts.AbstractBusinessDeleteService;
import com.xbongbong.pro.enums.lowcode.LowCodeExecuteServiceEnum;
import com.xbongbong.pro.lowcodeexecute.pojo.dto.LowCodeExecuteServiceDTO;
import com.xbongbong.pro.workflow.enums.WorkflowOperateEnum;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.paas.service.workflow.trigger.WorkflowFormDataOperationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/02/09 16:06
 */
public abstract class AbstractWorkOrderDeleteService extends AbstractBusinessDeleteService<WorkOrderFormEntityExt, WorkOrderEntity>  {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractWorkOrderDeleteService.class);

    @Resource
    private WorkflowFormDataOperationService workflowFormDataOperationService;
    @Resource
    private AbstractLowCodeExecuteService abstractLowCodeExecuteService;
    @Resource
    private FormDataInactiveRuleByDeleteBatchHelp formDataInactiveRuleByDeleteBatchHelp;

    /**
     * 失效时触发业务规则
     * @param formDataDeleteBatchDTO
     * @param businessRules
     */
    protected void inactiveRule(FormDataDeleteBatchDTO formDataDeleteBatchDTO, String businessRules, List<Long> deleteIds) throws XbbException {
        // 业务规则逻辑 start
        if (!Objects.equals(1, formDataDeleteBatchDTO.getIsBusinessRule())
                && Objects.equals("1", businessRules) && CollectionsUtil.isNotEmpty(deleteIds)) {
            InactiveRuleByDeleteBatchDTO inactiveRuleDTO = new InactiveRuleByDeleteBatchDTO();
            BeanUtil.copyProperties(formDataDeleteBatchDTO, inactiveRuleDTO);
            inactiveRuleDTO.setFormDataIdList(deleteIds);
            formDataInactiveRuleByDeleteBatchHelp.inactiveRuleByDeleteBatch(inactiveRuleDTO);
        }
        // 业务规则逻辑 end
    }

    /**
     * 低代码执行服务
     * @param dataIdList
     * @param saasMark
     * @param businessType
     * @param formId
     * @param corpid
     * @return void
     * @author 吴峰
     * @date 2021/3/18 20:59
     * @throws XbbException
     */
    protected void lowCodeExecuteService(List<Long> dataIdList, Integer saasMark, Integer businessType, Long formId, String corpid) throws XbbException {
        try {
            LowCodeExecuteServiceDTO lowCodeExecuteServiceDTO = new LowCodeExecuteServiceDTO();
            lowCodeExecuteServiceDTO.setAlias(LowCodeExecuteServiceEnum.DELETE.name());
            lowCodeExecuteServiceDTO.setBusinessType(businessType);
            lowCodeExecuteServiceDTO.setFormId(formId);
            lowCodeExecuteServiceDTO.setDataId(dataIdList);
            lowCodeExecuteServiceDTO.setSaasMark(saasMark);
            lowCodeExecuteServiceDTO.setCorpid(corpid);
            abstractLowCodeExecuteService.execute(lowCodeExecuteServiceDTO);
        } catch (Exception e) {
            LOG.error("listBatchServiceImpl.lowCodeExecuteService fail", e);
        }

    }

    protected void startWorkflow(FormDataDeleteBatchDTO formDataDeleteBatchDTO, String businessRules, List<Long>deleteIds) throws XbbException {
        //是否启用新工作流
        if (workflowFormDataOperationService.isEnableNewWorkflow(formDataDeleteBatchDTO,  deleteIds, WorkflowOperateEnum.DELETE_BATCH)) {
            // 业务规则逻辑 start
            inactiveRule(formDataDeleteBatchDTO, businessRules, deleteIds);
            return;
        }

        // 业务规则逻辑 start
        inactiveRule(formDataDeleteBatchDTO, businessRules, deleteIds);

    }
}
